import store from "@/store";
import { encrypt } from "@/utils";
import http from "@/utils/http";
import { HttpResponse, checkHttpError } from "./common";
import { FORM_DATA_AXIOS_CONFIG } from "./constants";

/**
 * 用户登录
 * @param username
 * @param password
 * @param code
 * @returns
 */
export async function loginByUsernameAndPassword(
  username: string,
  password: string,
  code: string
) {
  const form = new FormData();

  password = encrypt("pass" + password);

  form.append("username", username);
  form.append("password", password);
  form.append("code", code);

  const resp: HttpResponse = await http.post(
    "/User/login/",
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  checkHttpError(resp);
  return resp.detail;
}

/**
 * 获取用户菜单权限码
 */
export async function getMeunCodes(uid: string) {
  const resp: HttpResponse = await http.get(`/User/${uid}/showpopdommenu2/`);
  checkHttpError(resp);
  return resp.detail;
}

/**
 * 获取用户原子权限码
 * @param uid
 * @returns
 */
export async function getAtomicCodes(uid: string) {
  const resp: HttpResponse = await http.get(`/User/${uid}/showpopdomsmall2/`);
  checkHttpError(resp);
  return resp.detail;
}

export async function getMenuRender(uid: string) {
  const form = new FormData();
  form.append("insys", store.getters.system.insysid);

  const resp: HttpResponse = await http.post(
    `/User/${uid}/showpopdommenu5/`,
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  return checkHttpError(resp) ? resp.detail : false;
}
